package com.x.xiaoshuo.widget;

import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.RelativeSizeSpan;
import android.util.AttributeSet;

import com.x.xiaoshuo.R;

public class PrecentTextView extends android.support.v7.widget.AppCompatTextView {
    PercentDrawable percentDrawable;
    float[] values;
    ValueAnimator valueAnimator = new ValueAnimator();

    public PrecentTextView(Context context) {
        super(context);
        init(context, null);
    }

    public PrecentTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public PrecentTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    void init(Context context, AttributeSet attrs) {
        int weight = 10;
        if (attrs != null) {
            TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.PrecentTextView);
            try {
                weight = a.getDimensionPixelSize(R.styleable.PrecentTextView_cell_size, 10);
            } finally {
                a.recycle();
            }
        }
        percentDrawable = new PercentDrawable(weight, 0xFFeeeeee);
        percentDrawable.setContentColors(new int[]{0xFFFF583D, 0xFfc6ecb7}, new float[]{0.4f, 0.0f});
        setText(0.4 * 100 + "");
        valueAnimator.setFloatValues(0.1f, 1f);
        valueAnimator.setDuration(500);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float f = (float) animation.getAnimatedValue();
                float[] floats = new float[values.length];
                for (int i = 0; i < floats.length; i++) {
                    floats[i] = values[i] * f;
                }
                percentDrawable.setValues(floats);
                if (values != null) {
                    setText(floats[0] * 100 + "");
                }
                invalidate();
            }
        });
    }

    void setText(String str) {
        if (str.contains(".")) {
            str = str.substring(0, str.indexOf("."));
        }
        SpannableStringBuilder ssb = new SpannableStringBuilder(str + "%");
        ssb.setSpan(new RelativeSizeSpan(0.7f), str.length(), str.length() + 1, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        super.setText(ssb);
    }

    public void setContentColors(int[] colors, float[] values) {
        percentDrawable.setContentColors(colors, values);
        this.values = values;
        invalidate();
    }

    public void setContentColors(int[] colors) {
        percentDrawable.setContentColors(colors);
        invalidate();
    }

    public void setContentValue(float[] values) {
        percentDrawable.setValues(values);
        this.values = values;
        if (values != null) {
            setText(values[0] * 100 + "");
        }
        invalidate();
    }

    public void setAmiContentValue(float[] values) {
        this.values = values;
        startAnimation();
    }

    void startAnimation() {
        valueAnimator.start();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        percentDrawable.setCellSize(w);
        super.onSizeChanged(w, h, oldw, oldh);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        percentDrawable.draw(canvas);
        super.onDraw(canvas);
    }
}
